/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.servlet;

import it.unitn.science.matr155863_148346_145963.bccstore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Category;
import it.unitn.science.matr155863_148346_145963.bccstore.items.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */
public class LeftColSRV extends HttpServlet {

    
    private ArrayList<Category> categories;
    private String publicMenu;
    private String userMenu;
    private String sellerMenu;
    private static Logger log = Logger.getLogger(LeftColSRV.class);
    
    
    @Override
    public void init() throws UnavailableException{
            DBManager db=(DBManager)super.getServletContext().getAttribute("db_manager");
            categories=db.getCategories();
    }
    
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            User user=(User)request.getSession().getAttribute("user");
            out.println("<a href=\""+response.encodeURL(request.getContextPath()+"/contents/public/home")+"\" ><h1>Home</h1></a>");
            out.println("<h2>Menu</h2>");
            if(user!=null){
                if(user.getSeller()){
                    //SELLER MENU:
                    out.println("<ul>");
                    out.println("<li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/user/seller/AddProduct")+"\">Add product</a></li>");
                    out.println("<li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/user/seller/SoldProduct")+"\">Sold product</a></li>");
                    out.println("<li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/user/seller/ShowBillProductSold")+"\">ShowBillProductSold</a></li>");
                    out.println("</ul>");
                }
                //USER MENU:
                out.println("<ul>");
                out.println("<li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/user/ShowBill")+"\">Show bill</a></li>");
                out.println("</ul>");
            }
            //PUBLIC MENU:
            out.println("<ul><li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/public/SearchProducts")+"\">Search Products</a></li>");
            for(Category c:categories){
                out.println("<li><a href=\""+response.encodeURL(this.getServletContext().getContextPath()+"/contents/public/SearchProducts?category="+c.getId())+"\" >"+c.getName()+"</a></li>");
            }
            out.append("</ul>");
        }catch(Exception ex){
            log.error(ex);
            RequestDispatcher dispatcher=request.getRequestDispatcher("/contents/public/errorPage.html");
            dispatcher.include(request, response);
        } finally {            
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
